﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using TVGrid.TVGridService;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows;

namespace TVGrid.ViewModel
{

    public class LoginViewModel : ViewModelBase, IDataErrorInfo
    {
        public LoginViewModel()
        {
            UserName = "";
            LoginText = "Login | Register";
        }

        private string  _username;
        public string UserName
        {
            get { return _username; }
            set
            {
                _username = value;
                OnPropertyChanged("UserName");
            }
        }

        private Visibility _showLogin;
        public Visibility ShowLogin
        {
            get { return _showLogin; }
            set
            {
                _showLogin = value;
                OnPropertyChanged("ShowLogin");
            }
        }

        private string _loginText;
        public string LoginText
        {
            get { return _loginText; }
            set
            {
                _loginText = value;
                OnPropertyChanged("LoginText");
            }
        }

        public class LoginEventArgs  : EventArgs
        {
            public bool IsSuccess { get; set; }
        }

        public event EventHandler<LoginEventArgs> LoginAnnounce;

        private void OnLogin()
        {
            var handler = LoginAnnounce;
            if (handler == null) return;
            handler(this, new LoginEventArgs { IsSuccess = true });
        }

        private string _errorMag;
        public string ErrorMag
        {
            get { return _errorMag; }
            set
            {
                _errorMag = value;
                OnPropertyChanged("ErrorMag");
            }
        }

        private string _errorMagTt;
        public string ErrorMagTT
        {
            get { return _errorMagTt; }
            set
            {
                _errorMagTt = value;
                OnPropertyChanged("ErrorMagTT");
            }
        }

        private bool _isLoginExpanded;
        public bool IsLoginExpanded
        {
            get { return _isLoginExpanded; }
            set
            {
                _isLoginExpanded = value;
                OnPropertyChanged("IsLoginExpanded");
            }
        }

        #region LoginCommand
        RelayCommand loginCommand;
        public ICommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                {
                    loginCommand = new RelayCommand(p => this.Login(p),
                    p => this.CanLogin(p));
                }
                return loginCommand;
            }
        }

        private bool CanLogin(object p)
        {
            return this.UserName != "";
        }

        private object Login(object p)
        {
            if (p is PasswordBox && ((PasswordBox)p).Password != "" && UserName != "")
            {
                User.UserName = UserName;
                User.Password = ((PasswordBox)p).Password;
                try
                {
                    ServiceClient.GetUserIDCompleted += (s, e) =>
                    {
                        User.UserID = e.Result;
                        if (User.UserID != 0)
                        {
                            ShowLogin = Visibility.Collapsed;
                            LoginText = User.UserName;
                            IsLoginExpanded = false;
                            UserLoggedin();
                        }
                        else
                        {
                            ErrorMag = "Wrong credentials";
                            ErrorMagTT = "Chack your userneme and password";
                        }
                    };

                    ServiceClient.GetUserIDAsync(User.UserName, User.Password);
                }
                catch(Exception ex)
                {
                    ErrorMag = "Login failed";
                    ErrorMagTT = ex.Message;
                }
            }
            return null;
        }
        #endregion

        #region RegisterCommand
        RelayCommand registerCommand;
        public ICommand RegisterCommand
        {
            get
            {
                if (registerCommand == null)
                {
                    registerCommand = new RelayCommand(p => this.Register(p),
                    p => this.CanRegister(p));
                }
                return registerCommand;
            }
        }

        private bool CanRegister(object p)
        {
            return this.UserName != "";
        }

        private object Register(object p)
        {
            if (p is PasswordBox && ((PasswordBox)p).Password != "" && UserName != "")
            {
                User.UserName = UserName;
                User.Password = ((PasswordBox)p).Password;
                try
                {
                    ServiceClient.RegisterNewUserCompleted += (s, e) =>
                    {

                        User.UserID = e.Result;

                        if (User.UserID != 0)
                        {
                            ShowLogin = Visibility.Hidden;
                            LoginText = User.UserName;
                            IsLoginExpanded = false;
                            UserLoggedin();

                            MyMessenger.Publish(new LoginMsg { IsSuccess = true });
                        }
                    };

                    ServiceClient.RegisterNewUserAsync(User.UserName, User.Password);
                }
                catch (Exception ex)
                {
                    ErrorMag = "Registartion failed";
                    ErrorMagTT = ex.Message;
                }
            }
            return null;
        }
        #endregion

        #region LogoutCommand
        RelayCommand logoutCommand;
        public ICommand LogoutCommand
        {
            get
            {
                if (logoutCommand == null)
                {
                    logoutCommand = new RelayCommand(p => this.Logout(p),
                    p => this.CanLogout(p));
                }
                return logoutCommand;
            }
        }

        private bool CanLogout(object p)
        {
            return true;
        }

        private object Logout(object p)
        {
            User.UserName = null;
            User.Password = null;
            User.UserID = 0;
            IsLoginExpanded = false;
            ShowLogin = Visibility.Visible;
            LoginText = "Login | Register";
            UserLoggedout();
            return null;
        }
        #endregion

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get{
                string result = null;
                if (columnName == "UserName")
                {
                    if (string.IsNullOrEmpty(UserName))
                        result = "Please enter user name";
                }
                return result;
            }
        }
    }
}